package com.atguigu.tingshu.account.service.impl;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.IdUtil;
import com.atguigu.tingshu.account.mapper.RechargeInfoMapper;
import com.atguigu.tingshu.account.mapper.UserAccountMapper;
import com.atguigu.tingshu.account.service.RechargeInfoService;
import com.atguigu.tingshu.account.service.UserAccountService;
import com.atguigu.tingshu.common.constant.SystemConstant;
import com.atguigu.tingshu.common.execption.GuiguException;
import com.atguigu.tingshu.common.util.AuthContextHolder;
import com.atguigu.tingshu.model.account.RechargeInfo;
import com.atguigu.tingshu.model.account.UserAccount;
import com.atguigu.tingshu.vo.account.RechargeInfoVo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.Map;

@Service
@SuppressWarnings({"all"})
public class RechargeInfoServiceImpl extends ServiceImpl<RechargeInfoMapper, RechargeInfo> implements RechargeInfoService {

    @Autowired
    private RechargeInfoMapper rechargeInfoMapper;

    /**
     * 根据订单编号查询充值记录
     *
     * @param orderNo
     * @return
     */
    @Override
    public RechargeInfo getRechargeInfo(String orderNo) {
        LambdaQueryWrapper<RechargeInfo> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(RechargeInfo::getOrderNo, orderNo);
        return rechargeInfoMapper.selectOne(queryWrapper);
    }

    /**
     * 保存充值记录
     *
     * @param rechargeInfoVo
     * @return {orderNo:"充值记录订单编号"} 跟微信支付对接
     */
    @Override
    public Map<String, String> submitRecharge(RechargeInfoVo rechargeInfoVo) {
        //1.创建充值记录PO对象
        RechargeInfo rechargeInfo = new RechargeInfo();
        //2.属性赋值
        //2.1 前端提交参数
        rechargeInfo.setRechargeAmount(rechargeInfoVo.getAmount());
        rechargeInfo.setPayWay(rechargeInfoVo.getPayWay());
        //2.2 充值交易状态、用户ID
        rechargeInfo.setUserId(AuthContextHolder.getUserId());
        rechargeInfo.setRechargeStatus(SystemConstant.ORDER_STATUS_UNPAID);
        //2.3 生成充值唯一订单编号
        String today = DateUtil.today().replaceAll("-", "");
        String orderNo = "CZ" + today + IdUtil.getSnowflakeNextId();
        rechargeInfo.setOrderNo(orderNo);
        //2.4 保存充值金额
        rechargeInfoMapper.insert(rechargeInfo);
        //3.TODO 发送延迟任务 充值记录关闭
        Map<String, String> map = new HashMap<>();
        map.put("orderNo", orderNo);
        return map;
    }

    @Autowired
    private UserAccountService userAccountService;

    /**
     * 用户微信支付成功后完成余额充值,修改支付状态,新增变动明细
     *
     * @param orderNo 充值订单编号
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void rechargePaySuccess(String orderNo) {
        //1.根据充值订单编号查询充值记录 判读是否已经被处理
        LambdaQueryWrapper<RechargeInfo> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(RechargeInfo::getOrderNo, orderNo);
        RechargeInfo rechargeInfo = rechargeInfoMapper.selectOne(queryWrapper);
        if (SystemConstant.ORDER_STATUS_PAID.equals(rechargeInfo.getRechargeStatus())) {
            return;
        }
        //2.余额充值
        UserAccountMapper userAccountBaseMapper = (UserAccountMapper) userAccountService.getBaseMapper();
        int count = userAccountBaseMapper.rechargeAccount(rechargeInfo.getUserId(), rechargeInfo.getRechargeAmount());
        if (count == 0) {
            throw new GuiguException(500, "账户充值失败");
        }

        //3.修改充值订单支付状态：已支付
        rechargeInfo.setRechargeStatus(SystemConstant.ORDER_STATUS_PAID);
        rechargeInfoMapper.updateById(rechargeInfo);

        //4.新增账户变动日志
        userAccountService.saveUserAccountDetail(rechargeInfo.getUserId(), "充值：" + rechargeInfo.getRechargeAmount(), SystemConstant.ACCOUNT_TRADE_TYPE_DEPOSIT, rechargeInfo.getRechargeAmount(), rechargeInfo.getOrderNo());
    }
}
